<?php
namespace Home\Controller;
use Think\Controller;
class JieController extends CommonController {
		    public function index(){
               $id = I('get.id');

            $sql="select t1.*,t2.face,t2.nickname from question t1 inner join user t2 on t1.uid=t2.id where t1.id ='{$id}'";
            $res = M()->query($sql);

            $question = current($res);

               
            $this->autoAdd('question','view_num',"+1",$id);

            //评论
             $sql ="select t1.*,t2.nickname,t2.face from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid='{$id}' order by t1.create_time desc";
             
             $answers=M()->query($sql); 

		         $this->assign('question',$question);
		         $this->assign('answers',$answers); 
              

               //获取hot
              $this->getTopView();
              $this->getTopAnswer();

              // 收藏
              $uid=$_SESSION['id'];
          if (M('store')->where(array('qid'=>$id,'uid'=>$uid))->find()) {
                  $store=1;
              }else{
                  $store=2;
              }
            $this->assign('store',$store);

            // 查看是否点赞
            $time=[];
            foreach ($answers as $v) {
              $re=M('approve')->where(array('qid'=>$id,'approve-id'=>$_SESSION['id'],'answer-id'=>$v['uid'],'answer_create_time'=>$v['create_time']))->find();
              if (!empty($re)) {
                $time[]=$re['answer_create_time'];
              }
            }
            // var_dump($time);
             $this->assign('time',$time);

		          $this->display();
		    }


        // 发帖展示     
		    public function add(){
          
		      $question=D('verify')->getOneVerify();
		       $this->assign('question',$question);
		       $cates=D('category')->getCates();
		       $this->assign('cates',$cates);
		       $this->display();

		    }
		    
   
    //确认发帖
		    public function checkadd(){


              if(I('post.verify')!=$_SESSION['answer']) {
              	  $arr=array(
                       'error'=>'1',
                       'info' =>'验证不通过，重新答题'
              	  	);
              	  echo json_encode($arr);
              	  exit;
              }
                   $data=I('post.');
                   $data['uid']=$_SESSION['id'];
                   $data['create_time']=time();

               if ($id=M('question')->add($data)) {
               	$arr=array(
                      'error'=>0,
                      'info'=>"提交成功",
                      'url'=>U('home/jie/index',array('id'=>$id))
               		);
                 echo json_encode($arr);
                 exit;
		            }else{
                 	$arr=array(
                     'error'=>1,
                     'info'=>"发布失败",
           		    ); 
           		   echo json_encode($arr);
           	    	exit;
		          }
      }
 

		    public function uploadImage(){
               $upload = new \Think\Upload();// 实例化上传类
			    $upload->maxSize   =     3145728 ;// 设置附件上传大小
			    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			    $upload->rootPath  =     './Pic/'; // 设置附件上传根目录
			    $upload->savePath  =     ''; // 设置附件上传（子）目录
			    // 上传文件 
			    $info   =   $upload->upload();

			    if(!$info) {// 上传错误提示错误信息
			        echo $upload->getError();
			    }else{// 上传成功
			    	$info = current($info);

			    	$path = "__ROOT__/Pic/".$info['savepath'].$info['savename'];

			    	$arr = array(
			    		'code'=>0,
			    		'msg'=>'成功',
			    		'data'=>array(
			    			'src'=>$path,
			    			'title'=>'图片',
			    			)
			    		);

				    	echo json_encode($arr);
				    }
		    }


		     public function ajaxreply(){

              if (!$_SESSION['id']) {
                $arr=array(
                      'error'=>2,
                      'info'=>'请登录后进行回复',
                    );
                    echo json_encode($arr);
                    exit;
              }else{
		    	         $data=[];
                  $data['content']= I('post.content');
                  $data['uid']=$_SESSION['id'];
                  $data['create_time']=time();
                  $data['qid']=I('post.qid');
                  
                if(M('answer')->add($data)){
                	$this->autoAdd('question','answer_num',"+1",I('post.qid'));
                  $this->autoAdd('user','answer_num',"+1",$_SESSION['id']);
                   $arr=array(
                      'error'=>0,
                      'info'=>'回复成功',
                    );
                    echo json_encode($arr);
                    exit;
                     
                }else{
                  $arr=array(
                      'error'=>1,
                      'info'=>'回复失败'
                    );
                    echo json_encode($arr);
                    exit;
                }
              }  
		    }
        // 编辑帖子
        public function edit(){
          $id=I('get.id');//接收参数
          $res=M('question')->find($id);//找出要编辑的帖子
          $question=D('verify')->getOneVerify();//随机获取验证
          $this->assign('question',$question);
          $this->assign('res',$res);
          $cates=D('category')->getCates();//取出分类
          $this->assign('cates',$cates);
          $this->display();

        }

        // 确认修改编辑的帖子
        public function ajaxEdit(){

              //人类验证
              if(I('post.verify')!=$_SESSION['answer']) {
                  $arr=array(
                       'error'=>'1',
                       'info' =>'验证不通过，重新答题'
                    );
                  echo json_encode($arr);
                  exit;
              }
              $data['id']=I('post.id');
              $data['content']=I('post.content');
              $data['cid']=I('post.cid');
              $data['kiss_num']=I('post.kiss_num');
            if (M('question')->save($data)) {
                $arr=array(
                      'error'=>0,
                      'info'=>"修改成功",
                      'url'=>U('home/jie/edit',array('id'=>I('post.id')))
                  );
                 echo json_encode($arr);
                 exit;
                }else{
                  $arr=array(
                     'error'=>1,
                     'info'=>"修改失败",
                  ); 
                 echo json_encode($arr);
                  exit;
              }
        }
        // 收藏
        public function coll(){
           $qid= I('post.qid');
           $uid=$_SESSION['id'];
          if ($qid and $uid) {
            if (M('store')->where(array('qid'=>$qid,'uid'=>$uid))->find()) {
             M('store')->where(array('qid'=>$qid,'uid'=>$uid))->delete();
             $arr=array(
                'error'=>'3',
                'info'=>'取消收藏成功'
              );
             echo json_encode($arr);
             exit;
            }else{
              $data['qid']=$qid;
              $data['uid']=$uid;
              $data['create_time']=time();
              M('store')->add($data);
              $arr=array(
                 'error'=>'2',
                 'info'=>'收藏成功'
                );
              echo json_encode($arr);
              exit;
            }
          }else{
            $arr=array(
                 'error'=>'1',
                 'info'=>'参数不对!'
                );
              echo json_encode($arr);
              exit;
          }
        }
    public function so(){
        header("Content-type: text/html; charset=utf-8");    
        $q=I('get.q');
        Vendor('Pscws4.pscws4','','.class.php');//引入类文件
        $pscws = new \PSCWS4('utf8');//设置字符集,实例化类
        $pscws->set_dict('./Public/dict.utf8.xdb'); //路径
        $pscws->send_text($q);//向字典中检索字符
        $tops = $pscws->get_tops(3, 'n,v');//设置查询结果关键字数量
        if (empty($tops)) {
          $res=null;
          $this->assign('res',$res);
          $this->display();
           exit;
        }

        $sql = "select * from question where ";
         foreach ($tops as $row) {
            $sql .= " title like '%".$row['word']."%' or";

        }//查询语句

       $sql =  rtrim($sql,'or');

       $res = M()->query($sql);//执行查询
        foreach ($res as $k => $v) {

          foreach ($tops as $key => $row) {
            // var_dump($row['word']);
            $v['title'] =str_replace($row['word'], "<span style='color:red;background-color:#ffffff;margin:0px;padding:0px;'>{$row['word']}</span>", $v['title']);
            $res[$k]['title'] =$v['title'];
            // var_dump($res[$k]['title']);
          }
          
        }

       $this->assign('res',$res);

       $this->display();
    }
    // 点赞
    public function approve(){
          $id=$_SESSION['id'];

          $data['qid']=I('post.qid');
          $data['answer-id']=I('post.answer_id');
          $data['answer_create_time']=I('post.create_time');
          $data['approve-id']=$id;
           
          if (!$id) {
            $arr=array(
               'error'=>'1',
               'info'=>'请登录',
              );
            echo json_encode($arr);
            exit;
          }

          if (I('post.qid')) {
            if (M('approve')->where($data)->find()) {
               M('approve')->where($data)->delete();
               $arr=array(
               'error'=>'2',
               'info'=>'取消点赞成功'
                );
              echo json_encode($arr);
                  exit;
              }else{
              $data['create_time']=time();
               $res=M('approve')->add($data);
               $arr=array(
                'error'=>'3',
               'info'=>'点赞成功'
                );
               echo json_encode($arr);
                exit;
            }
          }
    }

    // 采纳
    public function  ajaxcaina(){
      $id=I('post.qid');
      $sql="update question set status ='已采纳' where id='{$id}'";
      // 执行问题状态
      if (M()->execute($sql)) {
        M('question')->
         $arr=array(
            'error'=>1,
            'info'=>'采纳成功'
          );
         echo json_encode($arr);
         exit;
      }else{
        $arr=array(
            'error'=>2,
            'info'=>'采纳不成功'
          );
         echo json_encode($arr);
         exit;
      }          
    }

}
